package popple.core
{
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.VertexBuffer3D;
	import flash.display3D.textures.Texture;
	import flash.utils.Dictionary;
	
	import mx.charts.chartClasses.RenderData;
	
	import popple.core.renders.BaseRender;

	public class RenderHelper
	{
		private var _context:Context3D;
		private var _vertexMap:Dictionary=new Dictionary;
		private var _textureMap:Dictionary=new Dictionary;
		private var _indexMap:Dictionary=new Dictionary;
		private var _bmpMap:Dictionary=new Dictionary;
		
		private var _renderMap:Dictionary=new Dictionary;
		static private var _instance:RenderHelper;
		public function RenderHelper(single:Single)
		{
			
		}
		static public function getInstance():RenderHelper
		{
			if(_instance==null)
				_instance=new RenderHelper(new Single);
			return _instance;
		}
		public function initialize(context:Context3D):void
		{
			_context=context;
		}
		public function getTexture(bmp:BitmapData,format:String,optimlize:Boolean):Texture
		{
			var texture:Texture;
			if(_textureMap[bmp]==null)
			{
				texture=_context.createTexture
					(
						bmp.width,
						bmp.height,
						format,
						optimlize
					);
				texture.uploadFromBitmapData(bmp);
				_textureMap[bmp]=texture;
				
			}
			
			return _textureMap[bmp];
		}
		public function removeTexture(bmp:BitmapData):void
		{
			if(_textureMap[bmp]!=null)
			{
				(_textureMap[bmp] as Texture).dispose();
				delete _textureMap[bmp]
			
			}
		}
		public function removeVertexBuffer(data:Vector.<Number>):void
		{
			if(_vertexMap[data]==null)
			{
				(_vertexMap[data] as VertexBuffer3D).dispose();
				delete _vertexMap[data];
			}
		}
		public function getIndexBuffer(indexs:Vector.<uint>):IndexBuffer3D
		{
			var indexBuffer:IndexBuffer3D;
			if(_indexMap[indexs]==null)
			{
				indexBuffer=_context.createIndexBuffer(indexs.length);
				indexBuffer.uploadFromVector(indexs,0,indexs.length);
				_indexMap[indexs]=indexBuffer;
			}
			return _indexMap[indexs];
		}
		public function getVertexBuffer(vertices:Vector.<Number>,num:uint):VertexBuffer3D
		{
			//trace("pp")
			var vertexBuffer:VertexBuffer3D;
			if(_vertexMap[vertices]==null)
			{
				vertexBuffer=_context.createVertexBuffer(vertices.length/num,num);
				vertexBuffer.uploadFromVector(vertices,0,vertices.length/num);
				_vertexMap[vertices]=vertexBuffer;
			}
			return _vertexMap[vertices];
		}
		public function getRender(render:Class):*
		{
			if(_renderMap[render]==null)
			{
				
				_renderMap[render]=new render(_context);;
			}
			return _renderMap[render];
		}
	}
}
class Single{}